/**
 * 详情对话框
 */
var InterfacelistInfoDlg = {
    data: {
        id: "",
        name: "",
        loc: "",
        paramId: "",
        returnSample: "",
        revision: "",
        createdBy: "",
        createdTime: "",
        updatedBy: "",
        updatedTime: ""
    }
};

var xSelect;

layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects', 'xmSelect', 'func'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var xmSelect = layui.xmSelect
    var func = layui.func;


    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/interfacelist/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('interfacelistForm', result.data);
    var res = result.data
    // console.log(res)


    /**
     * 给参数下拉列表赋值
     */

    $.ajax(Feng.ctxPath + "/interfaceparam/list", {
        success: function (obj) {
            // console.log(obj)
            strArr = obj.data;
            var newArr = [];
            for (var i = 0; i < strArr.length; i++) {
                newArr.push({name: strArr[i].param + '：' + strArr[i].detail, value: strArr[i].id,});
            }
            /**
             * 回显数据
             */
            arr = res.paramId.split(",")
            var arrReturn = [];//这里写入回传的参数
            // console.log(arr)
            for (var i = 0; i < arr.length; i++) {
                $.ajax(Feng.ctxPath + "/interfaceparam/list", {
                    data: {
                        id: parseInt(arr[i])
                    },
                    async: false,
                    success: function (data) {
                        // console.log(data)
                        arr1 = data.data;
                        arrReturn.push({name: arr1[0].param + '：' + arr1[0].detail, value: arr1[0].id,});
                    }
                })
            }

            xSelect = xmSelect.render({
                el: '#param',
                toolbar: {
                    show: true,
                    showIcon: true,
                },
                filterable: true,
                layVerify: 'required',
                tips: '如果没有想要的参数,点击左侧参数添加',
                searchTips: '如果没有想要的参数,点击左侧参数添加',
                paging: true,
                pageSize: 5,
                // remoteSearch: true,
                // pageRemote: true,
                data: newArr,
            })
            //数据回显
            // console.log(arrReturn);
            for (var i = 0; i < arrReturn.length; i++) {
                for (var j = 0; j < newArr.length; j++) {
                    if (arrReturn[i].name == newArr[i].name) {
                        arrReturn[i].__node = newArr[i].__node;
                    }
                }
            }
            // console.log(arrReturn);
            xSelect.setValue(arrReturn);

        }
    })


    /**
     * 点击参数添加参数
     */

    $("#addParam").click(function () {
        sessionStorage.setItem("listEdit", "listEdit");
        sessionStorage.setItem("id", res.id);
        admin.closeThisDialog()
        func.open({
            title: '',
            content: Feng.ctxPath + "/interfaceparam/add",
            cancel: function () {
                window.location.href = Feng.ctxPath + '/interfacelist/edit'
            },
            maxmin:true,
            success: function(layero){
                layero.find('.layui-layer-min').remove();
            },
        })
    })

    /**
     * 点击提交按钮
     */
    $("#btnSubmit").click(function () {
        var id = $("#id").val()
        // alert(id)
        var name = $("#name").val()
        var loc = $("#loc").val()
        var description = $('#description').val()
        var returnSample = $("#returnSample").val()
        var paramId = ''
        var param = xSelect.getValue();
        for (var i = 0; i < param.length; i++) {
            if (param.length == 1) {
                paramId = param[i].value.toString()
            } else {
                if (i < param.length - 1) {
                    paramId = paramId + param[i].value + ','
                }
                if (i == param.length - 1) {
                    paramId = paramId + param[i].value
                }
            }
        }
        $.ajax(Feng.ctxPath + "/interfacelist/editItem", {
            data: {
                id: id,
                name: name,
                loc: loc,
                returnSample: returnSample,
                paramId: paramId,
                description:description
            },
        })
        parent.location.reload()
    })

    /**
     * 文本域高度自适应
     */
    //jQuery实现textarea高度根据内容自适应
    $.fn.extend({
        txtaAutoHeight: function () {
            return this.each(function () {
                var $this = $(this);
                if (!$this.attr('initAttrH')) {
                }
                setAutoHeight(this).on('input', function () {
                    setAutoHeight(this);
                });
            });

            function setAutoHeight(elem) {
                var $obj = $(elem);
                return $obj.css({height: $obj.attr('initAttrH'), 'overflow-y': 'hidden'}).height(elem.scrollHeight);
            }
        }
    });
    //调用
    $(function () {
        $("#returnSample").txtaAutoHeight();
    });

});